#ifndef _CONF_PARSER_H
#define _CONF_PARSER_H

#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <unordered_map>
#include <regex>
#include "server.h"

typedef struct {
	std::string type;
	int expire;
} MIMEType;
typedef struct {
	std::string src,dest;
	std::regex reg;
} RewriteItem;
typedef struct {
	std::string src,dest;
} AliasItem;
typedef struct {
	int lb,ub;
	std::string url;
} ErrorDocItem;
typedef struct {
	std::string name;
	std::vector<RewriteItem> rr;
	std::vector<AliasItem> alias;
	std::vector<ErrorDocItem> errdocs;
} HostItem;
typedef struct {
	Address addr;
	//FCGI fcgi;
} FCGIItem;
typedef struct {
	Address addr;
	std::vector<HostItem *> hosts;
	std::unordered_map<std::string,FCGIItem *> fcgi;
	int threads;
} ServerItem;

typedef std::unordered_map<std::string,MIMEType *> MIMEMap;
typedef std::vector<ServerItem *> ServerList;

void AppendPath(std::string *,std::string);
std::string ExpandUser(std::string);
class ConfParser {
	public:
		ConfParser(std::string,bool);
		~ConfParser();
		virtual void Parse(void *);
	protected:
		std::ifstream is;
};
class MIMEParser:ConfParser {
	public:
		MIMEParser();
		void Parse(void *);
};
class MainParser:ConfParser {
	public:
		MainParser();
		void Parse(void *);
	private:
		bool GetArg(std::stringstream *,std::string *,bool);
};

#endif
